package com.shell.customer.base.cmd;

import com.shell.bee.base.exception.UnCheckMsgException;
import com.shell.bee.base.utils.StringUtil;
import com.shell.customer.accreditation.vo.UserVo;
import com.shell.customer.base.CusEvent;
import com.shell.customer.base.service.CusRecService;
import com.shell.customer.base.vo.CusRecVo;
import com.shell.customer.cfca.service.CfcaAuthTxService;
import com.shell.customer.po.CusAcct;
import com.shell.framework.command.CommandContext;
import com.shell.framework.command.ICommand;
import com.shell.governor.user.vo.PubUserVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CfcaAuthEntAcctForCusCmd implements ICommand<CusEvent> {
	@Autowired
	private CfcaAuthTxService cfcaAuthTxService;
	@Autowired
	private CusRecService cusRecService;

	public boolean execute(CommandContext<CusEvent> context) throws Exception {
		CusEvent event = (CusEvent) context.getSource();
		CusRecVo cusRecVo = event.getCusRecVo();
		PubUserVo pubUser = event.getUser();
		UserVo user = new UserVo();
		BeanUtils.copyProperties(pubUser, user);
		if (((!cusRecVo.isOperator()) || (!"ENTERPRISE".contains(cusRecVo.getCusType()))) && (!cusRecVo.isArtisan())) {
			try {
				String a = this.cfcaAuthTxService.authUser(user);
				if (!StringUtil.emptyAndNull(a)) {
					throw new UnCheckMsgException(a);
				}
				CusAcct cusAcct = event.getCusAcct();
				String result = this.cfcaAuthTxService.authEnt(cusRecVo, cusAcct, false);
				if (!StringUtil.emptyAndNull(result)) {
					throw new UnCheckMsgException(result);
				}
			} catch (Exception e) {
				throw new UnCheckMsgException("��������" + e.getMessage());
			}
		}
		return false;
	}
}
